package com.example.battle.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.battle.R;
import com.example.battle.db.AppDatabase;
import com.example.battle.db.BattleDao;
import com.example.battle.entity.FansName;
import com.example.battle.entity.User;
import com.example.battle.util.ActivityCollector;
import com.example.battle.util.MD5;
import com.example.battle.util.ToastUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Login extends AppCompatActivity implements View.OnClickListener {
    private EditText et_id,et_password;
    private Button login,register;
    private ImageView iv_eye,iv_more_account;
    private CheckBox cb_remember;
    private boolean passwordVisible = false;
    private ToastUtils toastUtils = new ToastUtils();
    private BattleDao battleDao;
    private static Login mInstance;
    private AppDatabase appDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
        setContentView(R.layout.activity_login);

        login = (Button) findViewById(R.id.login);
        register = (Button) findViewById(R.id.register);
        et_id = (EditText) findViewById(R.id.et_account_id);
        et_password = (EditText) findViewById(R.id.et_password);
        iv_eye = (ImageView) findViewById(R.id.iv_eye);
        iv_more_account = (ImageView) findViewById(R.id.iv_more_accout);
        cb_remember = (CheckBox) findViewById(R.id.cb_remember);

        //设置监听器
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        iv_eye.setOnClickListener(this);
        iv_more_account.setOnClickListener(this);

        mInstance = this;
        appDB = Room.databaseBuilder(this,AppDatabase.class,"battle")
                .addMigrations()
                .allowMainThreadQueries()
                .build();
        battleDao = appDB.battleDao();
    }

    @Override
    public void onClick(View v) {
        String id = et_id.getText().toString();
        String password = et_password.getText().toString();
        int num = 0;
        switch (v.getId()){
            //注册按钮的逻辑
            case R.id.register:
                num = battleDao.getUserNum();
                if (num == 0){
                    insertToFansName();
                }
                Intent intent = new Intent(Login.this, Register.class);
                startActivity(intent);
                break;
            //登录按钮的逻辑
            case R.id.login:
                num = battleDao.getUserNum();
                if (num == 0){
                    insertToFansName();
                }
                if (password.equals("") || id.equals("")){
                    toastUtils.showShort(Login.this,"账号或密码不能为空");
                    break;
                }
                boolean login_flag = false; //是否登录成功的标志，

                User user = battleDao.login(id);
                if (user == null){
                    toastUtils.showShort(Login.this,"该账号尚未注册");
                    break;
                }

                else password = MD5.md5(password);
                //密码正确则登录成功
                if (user.checkPassword(password)){
                    //启动主界面
                    Intent intent1 = new Intent(Login.this, MainActivity.class);
                    user.setPassword("");
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("user", user);
                    intent1.putExtras(bundle);
                    startActivity(intent1);
                    login_flag = true;
                    toastUtils.showShort(Login.this,"账户"+user.getUserId()+" 登录成功");
                    break;
                }

                if(login_flag == false){
                    toastUtils.showShort(Login.this,"登录失败，密码错误");
                }
                break;
            //隐藏密码功能
            case R.id.iv_eye:
                if(passwordVisible){  //如果可见，则转为不可见
                    iv_eye.setSelected(false);
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    passwordVisible = false;
                }else {  //如果不可见，则转为可见
                    iv_eye.setSelected(true);
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    passwordVisible = true;
                }
                break;
            //显示本地所有登录信息
            case R.id.iv_more_accout:
                List<User> users1 = battleDao.findAll();
                for(User u:users1) Log.d("user:",""+u.toString());
                break;
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    public static Login getInstance(){
        return mInstance;
    }

    public AppDatabase getAppDB(){
        return appDB;
    }

//    初始化界面那个粉丝球
    private void insertToFansName(){
        FansName[] fansNames = new FansName[14];
        FansName fansName1 = new FansName("星星","张杰");
        fansName1.setRegisterDate(new Date().toString());
        fansName1.setStatus(0);
        fansName1.setWeburi("https://baike.baidu.com/item/%E5%BC%A0%E6%9D%B0/256?fr=aladdin");

        FansName fansName2 = new FansName("爱丽丝","迪丽热巴");
        fansName2.setRegisterDate(new Date().toString());
        fansName2.setStatus(0);
        fansName2.setWeburi("https://baike.baidu.com/item/%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4/1180418?fr=aladdin");

        FansName fansName3 = new FansName("小汤圆","王源");
        fansName3.setRegisterDate(new Date().toString());
        fansName3.setStatus(0);
        fansName3.setWeburi("https://baike.baidu.com/item/%E7%8E%8B%E6%BA%90/5475062?fr=aladdin");

        FansName fansName4 = new FansName("小螃蟹","王俊凯");
        fansName4.setRegisterDate(new Date().toString());
        fansName4.setStatus(0);
        fansName4.setWeburi("https://baike.baidu.com/item/%E7%8E%8B%E4%BF%8A%E5%87%AF/75850?fr=aladdin");

        FansName fansName5 = new FansName("千纸鹤","易烊千玺");
        fansName5.setRegisterDate(new Date().toString());
        fansName5.setStatus(0);
        fansName5.setWeburi("https://baike.baidu.com/item/%E6%98%93%E7%83%8A%E5%8D%83%E7%8E%BA/221450?fr=aladdin");

        FansName fansName6 = new FansName("四叶草","TFBOYS");
        fansName6.setRegisterDate(new Date().toString());
        fansName6.setStatus(0);
        fansName6.setWeburi("https://baike.baidu.com/item/TFBOYS/9083733?fromtitle=tfboy&fromid=17199936&fr=aladdin");

        FansName fansName7 = new FansName("兴迷","张艺兴");
        fansName7.setRegisterDate(new Date().toString());
        fansName7.setStatus(0);
        fansName7.setWeburi("https://baike.baidu.com/item/%E5%BC%A0%E8%89%BA%E5%85%B4/3435810?fr=aladdin");

        FansName fansName8 = new FansName("小飞侠","肖战");
        fansName8.setRegisterDate(new Date().toString());
        fansName8.setStatus(0);
        fansName8.setWeburi("https://baike.baidu.com/item/%E8%82%96%E6%88%98/18866899?fr=aladdin");

        FansName fansName9 = new FansName("枫叶","张子枫");
        fansName9.setRegisterDate(new Date().toString());
        fansName9.setStatus(0);
        fansName9.setWeburi("https://baike.baidu.com/item/%E5%BC%A0%E5%AD%90%E6%9E%AB/6353405?fr=aladdin");

        FansName fansName10 = new FansName("小笼包","朱一龙");
        fansName10.setRegisterDate(new Date().toString());
        fansName10.setStatus(0);
        fansName10.setWeburi("https://baike.baidu.com/item/%E6%9C%B1%E4%B8%80%E9%BE%99/4038282?fr=aladdin");

        FansName fansName11 = new FansName("鞠骑","鞠婧祎");
        fansName11.setRegisterDate(new Date().toString());
        fansName11.setStatus(0);
        fansName11.setWeburi("https://baike.baidu.com/item/%E9%9E%A0%E5%A9%A7%E7%A5%8E/10201840#3");

        FansName fansName12 = new FansName("ikun","蔡徐坤");
        fansName12.setRegisterDate(new Date().toString());
        fansName12.setStatus(0);
        fansName12.setWeburi("https://baike.baidu.com/item/%E8%94%A1%E5%BE%90%E5%9D%A4/8511458?fr=aladdin");

        FansName fansName13 = new FansName("羊毛","杨洋");
        fansName13.setRegisterDate(new Date().toString());
        fansName13.setStatus(0);
        fansName13.setWeburi("https://baike.baidu.com/item/%E6%9D%A8%E6%B4%8B/11573?fr=aladdin");

        FansName fansName14 = new FansName("灯芯","邓伦");
        fansName14.setRegisterDate(new Date().toString());
        fansName14.setStatus(0);
        fansName14.setWeburi("https://baike.baidu.com/item/%E9%82%93%E4%BC%A6/10133303?fr=aladdin");

        fansNames[0] = fansName1;
        fansNames[1] = fansName2;
        fansNames[2] = fansName3;
        fansNames[3] = fansName4;
        fansNames[4] = fansName5;
        fansNames[5] = fansName6;
        fansNames[6] = fansName7;
        fansNames[7] = fansName8;
        fansNames[8] = fansName9;
        fansNames[9] = fansName10;
        fansNames[10] = fansName11;
        fansNames[11] = fansName12;
        fansNames[12] = fansName13;
        fansNames[13] = fansName14;
        battleDao.initFansName(fansNames);

    }

}
